package main

import (
	"errors"
	"io"
	"net/http"
	"net/url"
)

// Context 上下文
type Context struct {
	writer  http.ResponseWriter
	request *http.Request
	Method  string
	Pattern string
	params  map[string]string
	//内部维护一份查询参数数据
	cacheQuery url.Values
	//内部维护一份请求体数据
	cacheBody io.ReadCloser
}

func (c *Context) Params(key string) (string, error) {
	value, ok := c.params[key]
	if !ok {
		return "", errors.New("web:不存在")
	}
	return value, nil
}
func (c *Context) Query(key string) (string, error) {
	if c.cacheQuery == nil {
		c.cacheQuery = c.request.URL.Query()
	}
	get := c.cacheQuery.Get(key)
	return string(get[0]), nil
}
func NewContent(w http.ResponseWriter, r *http.Request) *Context {
	return &Context{writer: w, request: r, Method: r.Method, Pattern: r.URL.Path}
}
